package com.cysion.study.ui

import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.whiteTextTheme
import com.cysion.other.color
import com.cysion.targetfun._subscribe
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.R
import com.cysion.study.adapter.ChooseKnowListAdapter
import com.cysion.study.constant.BUNDLE_KEY
import com.cysion.study.entity.XitiListEntity
import com.cysion.study.helper.UserCache
import com.cysion.study.net.ApiClient
import com.cysion.study.widget.ShowToast
import kotlinx.android.synthetic.main.activity_modify_judge_ti.*
import kotlinx.android.synthetic.main.activity_modify_judge_ti.topbar
import kotlinx.android.synthetic.main.activity_modify_judge_ti.tv_submit

//编辑判断题
class ModifyJudgeTiActivity : BaseActivity() {
    override fun getLayoutId(): Int = R.layout.activity_modify_judge_ti
    private lateinit var dataAdapter: ChooseKnowListAdapter
    private val dataList: MutableList<String> = mutableListOf()
    private var curPage = 1
    private var courseId = ""
    private var pageType = ""
    private var obj: XitiListEntity? = null
    override fun initView() {
        courseId = intent.extras.getString("courseId")
        pageType = intent.extras.getString("type")
        whiteTextTheme(color(R.color.main_cl))
         topbar.apply {
             //1编辑，2新增
             if (pageType == "1") {
                 setTitle("编辑判断题")
                 obj = intent.extras.getSerializable("obj") as XitiListEntity?
                 modify()

             } else {
                 setTitle("添加判断题")
             }
//            setTexts("添加", TopBar.Pos.RIGHT)
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    finish()
                }else if (pos == TopBar.Pos.RIGHT) {
//                    startActivity_ex<AddKnowActivity>()//添加习题与知识

                    /*AlertSearch.search(self,""){ confirm: Int, start: String, end: String ->
                        CLog.i("@@@搜索="+start+end)
                        curPage=1
                        getDataList(curPage,start,end)
                    }*/
                }
            }
        }
        tv_submit.setOnClickListener {
            submit()
        }

    }

    private fun modify() {
        et_title.setText(obj?.title)
        if (obj?.answer == "1") {
            rb_right.isChecked = true
        } else {
            rb_wrong.isChecked = true
        }
    }

    private var id = ""

    private fun submit() {
        Alert.loading(self)
        var question = ""
        var answer = ""

        //对的话传1，错传2
        if (rb_right.isChecked) {
            answer = "1"
        }
        if (rb_wrong.isChecked) {
            answer = "2"
        }
        var title = et_title.text.toString().trim()
        println("###答案=" + question)
        var type="4"//类型：1、单选；2、多选,3、解答题；4、判断题

        //1编辑，2新增
        if (pageType == "1") {
            id = obj?.id.toString()
        }
        ApiClient.api.saveTi(id, courseId, question, answer, type, "1", "试题详解内容", title, UserCache.token)
            .compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    ShowToast("保存成功")
                    finish()
                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)

                }
            }
    }




    private fun getDataList(page: Int, start: String, end: String) {
        Alert.loading(self)
        ApiClient.api.getTutorOrderList(UserCache.token,"",curPage.toString()).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()


                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)

                }
            }

    }




    override fun closeMvp() {
    }

}
